package com.wiim.ui;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.CustomGoogleMapFragment;
import com.wiim.nhapnhap.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class DiaDiemFragment extends CustomGoogleMapFragment implements OnClickListener, OnMapClickListener {
	private Button btnTimkiem;
	private Button btnDanhSach;
	private CustomFragmentActivity activity;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.diadiem, container, false);
		initFeld(view);
//		initData();
		return view;
	}

	private void initData() {
		btnTimkiem.setOnClickListener(this);
		btnDanhSach.setOnClickListener(this);
		activity = (CustomFragmentActivity) getActivity();
		setMakerMap("", Utilities.getGlobalVariable(getActivity()).latlongDefault);

	}

	private void initFeld(View view) {
		btnDanhSach = (Button) view.findViewById(R.id.btnDanhSach);
		btnTimkiem = (Button) view.findViewById(R.id.btnTimkiem);

	}
	protected void setUpMapIfNeeded() {
		if (mMap == null) {
			mMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapMain));
			mMap = mMapFragment.getMap();
			if (mMap != null) {
				mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(getCurrentLocation(), 11));
				mMap.setOnMapClickListener(this);
			} else {
				Utilities.showDialogNoBack(getActivity(), "message_installGoogleMap").show();
			}
		}
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setUpMapIfNeeded();
		if (mMap != null) {
			initData();
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		releaseMap();
	}

	public void releaseMap() {
		try {
			FragmentManager manager = this.getFragmentManager();
			FragmentTransaction trans = manager.beginTransaction();
			SupportMapFragment frag = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapMain));
			if (null != frag) {
				trans.remove(frag);
			}
			trans.commit();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		if (v == btnDanhSach) {
			activity.replaceFragment(ConstantValue.DANHSACH_FRAGMENT, false);
		}
		if (v == btnTimkiem) {

		}

	}

	@Override
	public void onMapClick(LatLng arg0) {
		// TODO Auto-generated method stub
		
	}

}
